package com.zy.dp.iterator.example8;

import java.util.ArrayList;
import java.util.Collection;

public class ArrayIteratorImpl implements AggregationIterator
{
	private PayModel[] pms = null;
	private int index = 0;

	public ArrayIteratorImpl(SalaryManager aggregate)
	{
		this.pms = aggregate.getPays();
	}

	public boolean hasNext()
	{
		if (pms != null && index <= (pms.length - 1))
		{
			return true;
		}

		return false;
	}

	@Override
	public boolean hasPrevious()
	{
		if (pms != null && index > 0)
		{
			return true;
		}
		return false;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Collection getpage(int pageNum, int pageShow)
	{

		Collection collection = new ArrayList();
		int satrt = (pageNum - 1) * pageShow;
		int end = satrt + pageShow - 1;
		if (satrt < 0)
		{
			satrt = 0;
		}

		if (end > this.pms.length - 1)
		{
			end = this.pms.length - 1;
		}

		index = 0;

		while (hasNext() && index <= end)
		{
			if (index > satrt)
			{
				collection.add(pms[index]);

			}

			index++;

		}
		return collection;

	}

}
